<?php
#1.html调用php
#
#2.录入网站url，初始化要检索的网站，记录在服务器url_list.config文本文件中
#
#3.录入感兴趣的关键字，查找新闻链接
include('form.php');
include_once('openlist.php');
#echo 'crawlling.php:<br>'.$_SESSION['url_search'];
$SEARCH_LIST = $_SESSION['url_search'];
foreach($SEARCH_LIST as $k=>$v){
#echo '<br>'.$v;
#fetchWeb($v);
}
if(isset($_POST['keys']) and $_POST['submit']=='search'){
$_SESSION['_key'] = $_POST['keys'];
echo '<hr><br>crawlling-key:'.$_POST["keys"];
}
$REG_A_TARG = "/<a .*>.*<\/a>/";
$REG_URL = ''; 

$LIST_URL = array();

function getTargetObj($url){
Global $LIST_URL;
array_push($LIST_URL,$url);
return $str;
}
function getTargetUrlElement($reg,$str){
$_temp = array();
preg_match_all($reg,$str,$_temp);
return $_temp[0]; 
}
function filterUrlByKey($key,$a_target_url){
if(substr_count($a_target_url,$key)>0){
return $a_target_url;
}else{
return false;
}
}

function curl_getkd($url) {
    $meta = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    $html = curl_exec($ch);
    $info = curl_getinfo($ch);
    if ($html === false) {
        echo "cURL Error: " . curl_error($ch);
    } 
    curl_close($ch);
    preg_match_all("/<meta[^>]+name=\"([^\"]*)\"[^>]" . "+content=\"([^\"]*)\"[^>]*>/i", $html, $r, PREG_PATTERN_ORDER);
    for ($i = 0;$i < count($r[1]);$i++) {
        if (strtolower($r[1][$i]) == "keywords") $meta['keywords'] = $r[2][$i];
        if (strtolower($r[1][$i]) == "description") $meta['description'] = $r[2][$i];
    } 
    return $meta;
} 
// 测试
#$r = curl_getkd('http://www.qq.com/');
#echo '<pre>';
#print_r($r);
#echo '</pre>';

function fetchWeb($url){
$url = 'http://www.'.$url;
echo '<br>'.$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sohu.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$str = curl_exec($ch);
$output = $str;#mb_convert_encoding($str, 'utf-8', 'GBK,UTF-8,ASCII');
curl_close($ch);
echo "$output";
return '';
}

fetchWeb('baidu.com');

echo '<br><h3>Crawlling web!</h3>';
echo '<h4>^O^ </h4><br>';
echo '<br>Total:'.count($LIST_URL);
echo '<hr>';

#getTargetObj($REG_A_TARG,'ooooooooooouuuuuuuu <a title="" href="http://business.sohu.com/20150918/n421431946.shtml" target="_blank">中方会否受重创？9月会议你需要知道的一切！</a>dsf  $');
#getTargetObj($REG_A_TARG,'cccccccccccccccccc  <a title="" href="http://business.sohu.com/20150918/n421431946.shtml" target="_blank">中方会否受重创？9月你需要知道的一切</a>dsf  $');
#$hhaha = filterUrlByKey('会议x','ooooooooooouuuuuuuu <a title="" href="http://business.sohu.com/20150918/n421431946.shtml" target="_blank">中方会否受重创？9月会议你需要知道的一切！</a>dsf  $');
foreach($LIST_URL as $keys=>$values){ 
echo "<br>".$keys."==>".$values[0]; 
}
?>
